package cn.nawang.ebeim.uc;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class RestServer {
  private Server server;
  private int port = 8080;
  private static WebAppContext webAppContext;
  public RestServer() {}
  
  public RestServer(int port) {
    this.port = port;
  }

  public void start() throws Exception {
    server = new Server(port);
    webAppContext = new WebAppContext();

    webAppContext.setContextPath("/");
    String warPath = getClass().getResource("/").getFile().toString()+"bin/ebeim-ui.war";
    warPath = java.net.URLDecoder.decode(warPath,"utf-8");//读取文件进行路径编码，要不然路径中有中文会乱码、特殊字符会转义
    webAppContext.setWar(warPath);//在target下的classes的bin中
    webAppContext.setParentLoaderPriority(true);
    server.setHandler(webAppContext);

    server.start();
  }
  
  public static void destory(){
    webAppContext.destroy();
  }
  
  
  public void join() throws InterruptedException {
    server.join();
  }

  public static void main(String[] args) throws Exception {
    RestServer server = new RestServer();
    server.start();
    //System.out.println("OK");
  }
}
